package com.pimee.web.sys;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysMenu;
import com.pimee.model.vo.SysMenuVo;
import com.pimee.model.web.Ztree;
import com.pimee.service.admin.ISysMenuService;
import com.pimee.web.AbstractController;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping(value = "admin/sys/menu")
public class SysMenuController extends AbstractController {

	private String prefix = "admin/sys/menu";
	@Autowired
    private ISysMenuService menuService;
	
	@ApiOperation("查询菜单分页列表")
	@RequiresPermissions("system:menu:list")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Object listUser(@RequestParam Map<String, Object> params) {
		log.info("查询菜单列表");
		Long userId = ShiroUtils.getUserId();
		params.put("userId", userId);
		List<SysMenuVo> menuList = menuService.listMenu(params);
		return menuList;
	}
	
	@ApiOperation("进入新增菜单页")
    @GetMapping("/toAdd/{parentId}")
    public String toAdd(@PathVariable("parentId") Long parentId, ModelMap mmap) {
        SysMenu menu = null;
        if (0L != parentId) {
            menu = menuService.selectByKey(parentId);
        } else {
            menu = new SysMenu();
            menu.setMenuId(0L);
            menu.setMenuName("主目录");
        }
        mmap.put("menu", menu);
        return prefix + "/add";
    }
    
	@ApiOperation("保存新增菜单信息")
    @RequiresPermissions("system:menu:add")
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Validated SysMenu menu, ModelMap modelMap) {
    	String loginName = ShiroUtils.getLoginName();
    	menu.setCreateBy(loginName);
    	menu.setUpdateBy(loginName);
        menuService.insertMenu(menu);
        return this.setSuccessModelMap(modelMap);
    }

	@ApiOperation("进入修改菜单页")
    @GetMapping("/toEdit/{menuId}")
    public String toEdit(@PathVariable("menuId") Long menuId, ModelMap mmap) {
    	SysMenu menu = menuService.selectByKey(menuId);
    	SysMenu parentMenu = menuService.selectByKey(menu.getParentId());
    	SysMenuVo result = new SysMenuVo();
    	BeanUtil.copyProperties(menu, result);
    	if(parentMenu != null) {
    		result.setParentName(parentMenu.getMenuName());
    	}
        mmap.put("menu", result);
        return prefix + "/edit";
    }
    
	@ApiOperation("删除菜单")
    @RequiresPermissions("system:menu:remove")
    @GetMapping("/remove/{menuId}")
    @ResponseBody
    public Object remove(@PathVariable("menuId") Long menuId, ModelMap modelMap) {
        menuService.deleteMenuById(menuId);
        return setSuccessModelMap(modelMap);
    }
    
	@ApiOperation("保存修改菜单信息")
    @RequiresPermissions("system:menu:edit")
    @PostMapping("/edit")
    @ResponseBody
    public Object editSave(@Validated SysMenu menu, ModelMap modelMap) {
    	menu.setUpdateBy(ShiroUtils.getLoginName());
    	menuService.updateMenu(menu);
        return setSuccessModelMap(modelMap);
    }
    
	@ApiOperation("加载所有菜单列表树")
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData() {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = menuService.menuTreeData(userId);
        return ztrees;
    }

	@ApiOperation("选择菜单树")
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap modelMap) {
    	SysMenu menu = menuService.selectByKey(menuId);
    	SysMenu parentMenu = menuService.selectByKey(menu.getParentId());
    	SysMenuVo result = new SysMenuVo();
    	BeanUtil.copyProperties(menu, result);
    	if(parentMenu != null) {
    		result.setParentName(parentMenu.getMenuName());
    	}
    	modelMap.put("menu", result);
        return prefix + "/tree";
    }
    
	@ApiOperation("加载角色菜单列表树")
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(Long roleId) {
    	Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = menuService.roleMenuTreeData(userId, roleId);
        return ztrees;
    }
}
